﻿#include "StdAfx.h"
#include "IScharr.h"
//OpenCV libs
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv/cv.h"

const char * const IScharr::FunctionNameIScharr = "Scharr";

IScharr::IScharr(void) 
{
	_fnApi.SetFunctionName(IScharr::FunctionNameIScharr);
	_fnApi.SetFunctionDescription("Calculates the first x or y image derivative using Scharr operator");
									//inputs
	_fnApi.AddParamDef(new ProcParamDefInt(0, "ddepth", "The structuring element used for dilation. If element=Mat() , a 3\times 3 rectangular structuring element is used", ProcParamDef::input, 0, 500 ));
	_fnApi.AddParamDef(new ProcParamDefInt(1, "xorder", "Position of the anchor within the element. The default value -1 means that the anchor is at the element center", ProcParamDef::input, -500, 500 ));
	_fnApi.AddParamDef(new ProcParamDefInt(2, "yorder", "Position of the anchor within the element. The default value -1 means that the anchor is at the element center", ProcParamDef::input, -500, 500 ));
	_fnApi.AddParamDef(new ProcParamDefDouble(3, "scale", "The number of times dilation is applied", ProcParamDef::input, 0, 500 ));
	_fnApi.AddParamDef(new ProcParamDefInt(4, "delta", "The pixel extrapolation method", ProcParamDef::input, 0, 500 ));
	//_fnApi.AddParamDef(new ProcParamDefString(5, "borderValue", "The border value in case of a constant border.", ProcParamDef::input, 0, 500 ));
									//outputs
	_fnApi.AddParamDef(new ProcParamDefInt(0, ProcParam::attribOutputParamNameResult, "0 is success", ProcParamDef::output, 0, 1));

	_fnApi.Validate();
}


IScharr::~IScharr(void)
{
	
}


bool IScharr::Process(ImageProcStep *step, bool DebugMode)
{	
	bool rc = false;
	if ((step != '\0') && (step->IsValid()))
	{
		
		ProcParamInt *ddepth = dynamic_cast<ProcParamInt *>(step->GetInputs()->GetParam(0));	//Check the Param number
		ProcParamInt *xorder = dynamic_cast<ProcParamInt *>(step->GetInputs()->GetParam(1));
		ProcParamInt *yorder = dynamic_cast<ProcParamInt *>(step->GetInputs()->GetParam(2));
		ProcParamDouble *scale = dynamic_cast<ProcParamDouble *>(step->GetInputs()->GetParam(3));
		ProcParamInt *delta = dynamic_cast<ProcParamInt *>(step->GetInputs()->GetParam(4));

		//ProcParamInt *result = dynamic_cast<ProcParamInt *>(step->GetOutputs()->GetParam(0));		//Check the type and check the DefType
		if  (ddepth != '\0' && xorder != '\0' && yorder != '\0' && scale != '\0' && delta != '\0')
		{
			cv::Mat output;
			cv::Mat input = cv::imread(step->GetIntermediateImagePathFilename());
						
			cv::Scharr(input, output,int(ddepth->GetValue()) , int(xorder->GetValue()),int(yorder->GetValue()), double(scale->GetValue()), int(delta->GetValue()));

			cv::imwrite(step->GetIntermediateImagePathFilename(), output);

			input.release();
			//result->SetValue(0);
			rc = true;
		}
	}
	return rc;
}
